Completed
Push — master ( a3b87b...c9d36f )
by Andreas
14:59
created

dialog.js ➔ close   A

Complexity

Conditions 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
var dialog;
2
3
function refresh_opener(url) {
4
    if (url === undefined) {
5
        url = window.parent.location.href;
6
    }
7
    var button = window.parent.$('[data-dialog="dialog"][data-refresh-opener].active');
8
9
    if (button.length > 0) {
10
        if (   button.data('refresh-opener') === false
11
            && button.closest('.ui-tabs').length === 0) {
12
            close();
13
            return;
14
        }
15
        url = window.parent.location.href;
16
    }
17
    window.parent.location.href = url;
18
}
19
20
function close(data) {
21
    if (dialog) {
22
        dialog
23
            .trigger('dialogsaved', [data])
24
            .dialog('close');
25
    }
26
}
27
28
var extra_buttons = [];
29
function add_dialog_button(url, label, options) {
30
    var button = {
31
        text: label,
32
        'data-action': url,
33
        'class': 'dialog-extra-button',
34
        click: function(){}
35
    };
36
    $.each(options, function(key, value) {
37
        button[key] = value;
38
    });
39
    extra_buttons.push(button);
40
}
41
42
function add_post_button(url, label, options) {
43
    var button = {
44
        text: label,
45
        'class': 'dialog-extra-button',
46
        click: function() {
47
            var form = $('<form action="' + url + '" method="post"></form>'),
48
                dialog = window.parent.$('#midcom-dialog');
49
            $.each(options, function(key, value) {
50
                form.append($('<input type="hidden" name="' + key + '">').val(value));
51
            });
52
            form.appendTo('body').submit();
53
            dialog.dialog('option', 'buttons', []);
54
        }
55
    };
56
    extra_buttons.push(button);
57
}
58
59
function add_item(data) {
60
    if (dialog) {
61
        var widget_id = dialog.attr('id').replace(/_creation_dialog/, '');
62
        window.parent.midcom_helper_datamanager2_autocomplete.add_result_item(widget_id, data);
63
        close(data);
64
    }
65
}
66
67
function attach_to_parent_dialog(dialog) {
68
    let buttons = [];
69
    dialog.dialog('option', 'title', document.title);
70
    dialog.css('visibility', 'visible');
71
72
    $(window).on('unload', function() {
73
        dialog.nextAll('.ui-dialog-buttonpane').find('button')
74
            .prop('disabled', true)
75
            .addClass('ui-state-disabled');
76
    });
77
78
    if ($('.midcom-view-toolbar li').length > 0) {
79
        $('.midcom-view-toolbar li').each(function() {
80
            var btn = $(this).find('a'),
81
                options = {
82
                    click: function() {
83
                        btn.get(0).click();
84
                        btn.addClass('active');
85
                    }
86
                };
87
88
            add_dialog_button(btn.attr('href'), btn.text(), options);
89
        });
90
    }
91
92
    if ($('.datamanager2 .form_toolbar > *').length > 0) {
93
        $('.datamanager2 .form_toolbar > *').each(function() {
94
            var btn = $(this);
95
            buttons.push({
96
                text: btn.val() || btn.text(),
97
                click: function() {
98
                    if (btn.hasClass('cancel')) {
99
                        dialog.dialog('close');
100
                    } else {
101
                        btn.click();
102
                    }
103
                }
104
            });
105
        });
106
    }
107
    if (extra_buttons.length > 0) {
108
        buttons = extra_buttons.concat(buttons);
109
    }
110
111
    // This doesn't work under certain circumstances when flexbox is used somewhere in the page:
112
    // dialog.dialog('option', 'buttons', buttons);
113
    // @todo: The root of the problem seems to be that jquery can't set the content element
114
    // to a height of 0, so at some point this could be filed as a bug against their repo. Latest
115
    // stable (3.4.1) is affected. For now, we just copy the relevant part from jqueryui's
116
    // _createButtons method..
117
118
    var buttonset = dialog.nextAll('.ui-dialog-buttonpane').find('.ui-dialog-buttonset').empty();
119
120
    $.each(buttons, function (name, props) {
121
        var click, buttonOptions;
122
        props = $.isFunction(props) ? {click: props, text: name} : props;
123
124
        // Default to a non-submitting button
125
        props = $.extend({type: "button"}, props);
126
127
        // Change the context for the click callback to be the main element
128
        click = props.click;
129
        buttonOptions = {
130
            icon: props.icon,
131
            iconPosition: props.iconPosition,
132
            label: props.text
133
        };
134
135
        delete props.click;
136
        delete props.icon;
137
        delete props.iconPosition;
138
        delete props.text;
139
140
        $('<button></button>', props)
141
            .button(buttonOptions)
142
            .appendTo(buttonset)
143
            .on('click', function() {
144
                click.apply(dialog[0], arguments);
145
            });
146
    });
147
}
148
149
if (window.frameElement) {
150
    dialog = window.parent.$(window.frameElement.parentNode);
151
    window.addEventListener('DOMContentLoaded', function() {
152
        dialog.find(' > .fa-spinner').hide();
153
        if (window.hasOwnProperty('$')) {
154
            $('body').on('submit', '.midcom-dialog-delete-form', function(e) {
155
                e.preventDefault();
156
                var form = $(this).detach().appendTo(dialog);
157
158
                form.find('input[name="referrer"]')
159
                    .val(window.parent.location.href);
160
161
                //somehow, the original submit button breaks when detaching
162
                form.append($('<input type="hidden" name="' + form.find('input[type="submit"]').attr('name') + '" value="x">'))
163
                    .submit();
164
            });
165
            attach_to_parent_dialog(dialog);
166
        }
167
    });
168
} else if (window.hasOwnProperty('$')) {
169
    $('.midcom-view-toolbar, .datamanager2 .form_toolbar').show();
170
}
171